//
//  MJ_ReportHeadView.m
//  UEAssistant
//
//  Created by mac on 2018/1/8.
//  Copyright © 2018年 youyixinxi. All rights reserved.
//

#import "MJ_ReportHeadView.h"
#import "UIColor+Extend.h"
#import "UIImage+Color.h"
@interface MJ_ReportHeadView ()

@property (weak, nonatomic) IBOutlet UIButton *startTimeBtn;

@property (weak, nonatomic) IBOutlet UIButton *endTimeBtn;

@property (weak, nonatomic) IBOutlet UIButton *today;

@property (weak, nonatomic) IBOutlet UIButton *yesterday;

@property (weak, nonatomic) IBOutlet UIButton *theDayBeforeYesterday;

@property (weak, nonatomic) IBOutlet UIButton *week;

@property (weak, nonatomic) IBOutlet UIButton *month;

@property (weak, nonatomic) IBOutlet UILabel *startWeekDay;

@property (weak, nonatomic) IBOutlet UILabel *endWeekDay;

@property (strong, nonatomic)UIButton *selectedButton;
@end

@implementation MJ_ReportHeadView

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _selectedButton = self.today;
    [self selectedButtonUIChangedBtn:self.today];
    [self changeButtonTitleWithBeginDate:[NSDate date] endDate:[NSDate date]];
}

- (void)changeButtonTitleWithBeginDate:(NSDate *)begin endDate:(NSDate *)end
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM-dd";
    [self.startTimeBtn setTitle:[formatter stringFromDate:begin] forState:UIControlStateNormal];
    [self.endTimeBtn setTitle:[formatter stringFromDate:end] forState:UIControlStateNormal];
    self.startWeekDay.text = [self getWeekdayWithDate:begin];
    //截止日期星期
    self.endWeekDay.text = [self getWeekdayWithDate:end];
}
//计算周几
- (NSString *)getWeekdayWithDate:(NSDate *)date {
    //计算周几
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *startComponents = [calendar components:NSCalendarUnitWeekday fromDate:date];
    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
    nf.numberStyle = NSNumberFormatterSpellOutStyle;
    NSString *startString;
    if (startComponents.weekday-1 == 0) {
        startString = @"日";
    }else{
        startString = [nf stringFromNumber:[NSNumber numberWithFloat:startComponents.weekday-1]];
    }
    return [NSString stringWithFormat:@"周%@",startString];
}

- (IBAction)start:(id)sender {
    
    [_delegate clickWithType:MJTimeSelectTypeStart button:sender view:self];
}

- (IBAction)end:(id)sender {
    [_delegate clickWithType:MJTimeSelectTypeEnd button:sender view:self];
    
}

- (IBAction)conditionPressed:(UIButton *)sender {
    if (_selectedButton != sender) {
        MJTimeSelectType type;
        if ([sender isEqual:self.today]) {
            type = MJTimeSelectTypeToday;
            self.today.selected = !self.today.selected;
        }else if ([sender isEqual:self.yesterday]) {
            type = MJTimeSelectTypeYesterday;
            
        }else if ([sender isEqual:self.theDayBeforeYesterday]) {
            type = MJTimeSelectTypeTheDayBeforeYesterday;
            
        }else if ([sender isEqual:self.week]) {
            type = MJTimeSelectTypeWeek;
            
        }else {
            type = MJTimeSelectTypeMonth;
        }
        [_delegate clickWithType:type button:sender view:self];
        [self selectedButtonUIChangedBtn:sender];
        _selectedButton = sender;
    }

}


- (void)selectedButtonUIChangedBtn:(UIButton *)sender {
    
    NSArray *buttonArray = @[self.today,self.yesterday,self.theDayBeforeYesterday,self.week,self.month];
    
    for (UIButton *btn in buttonArray) {
        
        [btn setBackgroundImage:[UIImage createImageWithColor:[UIColor colorWithHexString:@"#0084ff"]] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor colorWithHexString:@"#474747"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        
        if ([btn isEqual:sender]) {
            btn.layer.borderWidth = 0;
            btn.layer.borderColor = [UIColor whiteColor].CGColor;
            btn.selected = YES;
        }else{
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = [UIColor colorWithHexString:@"#bdbdbd"].CGColor;
            btn.selected = NO;
        }
    }
}

- (void)setIsSelectRange:(BOOL)isSelectRange {
    _isSelectRange = isSelectRange;
    if (isSelectRange) {
        _selectedButton = nil;
        [self selectedButtonUIChangedBtn:nil];
    }
}

@end
